﻿using AuthorityCenter.Model;
using Ldt.SettingCenter.Core;
using LdtCommonFreeSql;
using LdtCommonLibraryHttp;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using TxtSearch.Bll;
using TxtSearch.Model;

namespace TxtSearch.Webapi.Controllers
{
    /// <summary>
    /// Class TxtSearchController.
    /// Implements the <see cref="Microsoft.AspNetCore.Mvc.ControllerBase" />
    /// </summary>
    /// <seealso cref="Microsoft.AspNetCore.Mvc.ControllerBase" />
    [ApiController]
    [Route("api/main")]
    public class MainController : ControllerBase
    {
        private readonly ILogger<MainController> logger;
        private readonly ServiceBll serviceBll;
        /// <summary>
        /// Initializes a new instance of the <see cref="MainController"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="freesqlDb">The freesql database.</param>
        /// <param name="settingCenter">The setting center.</param>
        /// <param name="serviceBll"></param>
        public MainController(ILogger<MainController> logger, FreesqlDb freesqlDb, ISettingCenter settingCenter, ServiceBll serviceBll, IHttpHelper httpHelper)
        {
            this.logger = logger;
            this.serviceBll = serviceBll;
        }
        /// <summary>
        /// Creates the datas by path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="isManySign">if set to <c>true</c> [is many sign].</param>
        /// <param name="modelCode">The model code.</param>
        /// <returns></returns>
        [HttpGet("createdatabypath")]
        public async Task<ActionResult> CreateDatasByPath(string path, bool isManySign,string modelCode)
        {
            await serviceBll.CreateDatasByPath(path, isManySign,modelCode);
            return Ok("1");
        }
        /// <summary>
        /// Creates the datas by modelcode.
        /// </summary>
        /// <param name="modelCode">The model code.</param>
        /// <returns></returns>
        [HttpGet("CreateDatasBymodelcode")]
        public async Task<ActionResult> CreateDatasBymodelcode(string modelCode)
        {
            await serviceBll.CreateDatasByModel(modelCode);
            return Ok("1");
        }
        /// <summary>
        /// Creates the datas by modelcode.
        /// </summary>
        /// <param name="modelCode">The model code.</param>
        /// <returns></returns>
        [HttpGet("CreateDatasBymodelcodeTable")]
        public async Task<ActionResult> CreateDatasBymodelcodeTable(string modelCode)
        {
            await serviceBll.CreateDatasByModelTable(modelCode);
            return Ok("1");
        }
        [HttpGet("createcsv")]
        public async Task<ActionResult> CreateCsv(string modelCode)
        {
            await serviceBll.CreateCsv(modelCode);
            return Ok("1");
        }

        [HttpPost("searchdata")]
        public async Task<ActionResult> SearchData(Classifydatas data,string modelCode)
        {
            var r = await serviceBll.SearchDatas(data.Content, modelCode);
            return Ok(r);
        }
        [HttpPost("ClassifyRecognition")]
        public async Task<ActionResult> GetSearchResults(RequestParam obj)
        {
            //string TestText, string modelCode
            var modelCode = obj.ModelCode == "dqqingxing_tetrievalbased"? "labeldqqingxing" : obj.ModelCode;
            var r = await serviceBll.GetSearchResults(obj.TestText, modelCode, null);
            return Ok(r);
        }
        [HttpGet("validdata")]
        public async Task<ActionResult> ValidData(string modelName)
        {
            await serviceBll.ValidData(modelName);
            return Ok("");
        }
        [HttpGet("createtraindata")]
        public async Task<ActionResult> CreateTrainData(string modelName)
        {
            await serviceBll.CreateTrainData(modelName);
            return Ok("");
        }
        [HttpGet("ConvertPathDataByModel")]
        public async Task<ActionResult> ConvertPathDataByModel(string modelName)
        {
            await serviceBll.ConvertPathDataByModel(modelName);
            return Ok("");
        }
        [HttpGet("createtraindata2")]
        public async Task<ActionResult> CreateTrainData2(string modelName)
        {
            await serviceBll.CreateTrainData2(modelName);
            return Ok("");
        }
    }
    //DELETE from embedding where md5 in (SELECT md5 from classifydatas where modelCode='dqqingxing')
    //select a.id,a.firstid,a.twoid, a.content as content1,b.content as content2,a.type as type1,b.type as type2 FROM classifydatas a INNER JOIN (select id,content,type from classifydatas) b on a.firstid=b.id  where modelCode='dqqingxing' and state='valid' and a.type!=b.type;
    //select a.content as content1, b.content as content2, a.type as type1, b.type as type2 FROM classifydatas a INNER JOIN (select id, content, type from classifydatas) b on a.twoid=b.id where modelCode='dqqingxing' and state = 'train' and a.type!=b.type;
}
